﻿@model IEnumerable<wdsDeApp2.Models.CarRecord>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("添加", "Create")
</p>
@using (Html.BeginForm("Index", "MuHome", FormMethod.Get))
{
    @Html.TextBox("carNO", null, htmlAttributes: new { @placeholder = "车牌号" })
    @Html.TextBox("mobile", null, htmlAttributes: new { @placeholder = "电话" })
    <input type="submit" />
}
<table class="table">
    <tr>
        <td>
            车辆编号
        </td>
        <th>
            车主姓名
        </th>
        <th>
            出入小区
        </th>
        <th>
            车辆名称
        </th>
        <th>
            车牌号
        </th>
        <th>
            车型
        </th>
        <th>
            电话
        </th>
        <th>
            驶入/使出时间
        </th>
        <th>
            进出状态
        </th>
        <th>
            停车类型
        </th>
        <th>
            操作
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.CarId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @{
                    var list = ViewBag.cName as Dictionary<int, string>;
                    var cName = list.FirstOrDefault(u => u.Key == item.CId).Value;
                }
                @cName
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CarName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CarNo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CarType)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Mobile)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DriveTime)
            </td>
            <td>
                @{
                    string state = "";
                    if (item.State == 1)
                    {
                        state = "驶入";
                    }
                    else
                    {
                        state = "驶出";
                    }
                }
                @state
            </td>
            <td>
                @{
                    string type = "";
                    if (item.Type == 1)
                    {
                        type = "已授权";
                    }
                    else
                    {
                        type = "临时停车";
                    }
                }
                @type
            </td>
            <td>
                <button onclick="Delete(@item.CarId)">删除</button>
                @if (item.Type == 2)
                {
                    <button onclick="Update(@item.CarId)">授权</button>
                }
            </td>
        </tr>
    }
</table>
@section Scripts {
    <script>
        function Delete(carId) {
            if (confirm("确定要删除编号为" + carId + "的信息吗?")) {
                $.post("/MuHome/Delete", { CarId: carId }, function (data) {
                    switch (data) {
                        case 0:
                            console.log("失败");
                            break;
                        case 1:
                            alert("删除成功");
                            location = "/MuHome/Index";
                            break;
                    }
                })
            }
        }

        function Update(carId) {
            if (confirm("确定要修改编号为" + carId + "的信息吗?")) {
                $.post("/MuHome/Update", { CarId: carId }, function (data) {
                    switch (data) {
                        case 0:
                            console.log("失败");
                            break;
                        case 1:
                            alert("修改成功");
                            location = "/MuHome/Index";
                            break;
                    }
                })
            }
        }
    </script>
}
